<template>
  <div class="">
    <div class="pc-home"></div>
    <div class="phone-home">
      <van-nav-bar
        title="历史订单"
        left-text="返回"
        left-arrow
        @click-left="$router.go(-1)"
      />
      <van-tabs v-model="active">
        <van-tab title="全部订单">
          <div class="orderitem" v-for="item in templist" :key="item.sum">
            <h4>订单创建时间:{{ item.date | formatDate }}</h4>
            <div class="imgbox">
              <img v-for="img in item.imglist" :src="img" :key="img" alt="" />
            </div>
            <p>
              <span>总计:{{ item.imglist.length }}件</span>
              <span class="price">订单总额:￥{{ item.sum }}</span>
            </p>
          </div>
        </van-tab>
        <van-tab title="已发货">
          <div class="orderitem" v-for="item in templist" :key="item.sum">
            <h4>订单创建时间:{{ item.date | formatDate }}</h4>
            <div class="imgbox">
              <img v-for="img in item.imglist" :src="img" :key="img" alt="" />
            </div>
            <p>
              <span>总计:{{ item.imglist.length }}件</span>
              <span class="price">订单总额:￥{{ item.sum }}</span>
            </p>
          </div>
        </van-tab>
        <van-tab title="待发货">
          <div class="orderitem" v-for="item in templist" :key="item.sum">
            <h4>订单创建时间:{{ item.date | formatDate }}</h4>
            <div class="imgbox">
              <img v-for="img in item.imglist" :src="img" :key="img" alt="" />
            </div>
            <p>
              <span>总计:{{ item.imglist.length }}件</span>
              <span class="price">订单总额:￥{{ item.sum }}</span>
            </p>
          </div>
        </van-tab>
      </van-tabs>
    </div>
  </div>
</template>
<script>
import { getorder } from '@/api/index';
export default {
  data() {
    return {
      orderlist: [],
      imglist: [],
      active: 0,
      templist: [],
    };
  },
  created() {
    getorder({ email: sessionStorage.getItem('email') }).then((res) => {
      console.log(res);
      var temp = [];
      res.data.data.forEach((item) => {
        // JSON.parse(item.goods).forEach((item) => {
        //   this.orderlist.push(item);
        // });
        temp.push([
          ...JSON.parse(item.goods),
          item.sum,
          item.status,
          item.datetime,
        ]);
      });
      // console.log(temp);
      temp.forEach((item) => {
        // this.orderlist.push(item[item.length - 1]);
        if (item.length > 4) {
          var obj = {};
          obj.sum = item[item.length - 3];
          obj.status = item[item.length - 2];
          obj.date = item[item.length - 1];
          var arr = [];
          for (let i = 0; i < item.length - 3; i++) {
            // console.log(item[i].img[0]);
            arr.push(item[i].img[0]);
          }
          obj.imglist = arr;
          this.orderlist.push(obj);
        } else {
          var obj1 = {};
          obj1.sum = item[1];
          obj1.status = item[2];
          obj1.date = item[3];
          var arr1 = [];
          arr1.push(item[0].img[0]);
          obj1.imglist = arr1;
          this.orderlist.push(obj1);
        }
      });
      this.templist = this.orderlist;
      console.log(this.orderlist);
    });
  },
  watch: {
    active: {
      deep: true,
      immediate: true,
      handler(nv) {
        if (nv == 0) {
          this.templist = this.orderlist;
        } else {
          this.templist = this.orderlist;
          this.templist = this.templist.filter((item) => {
            return item.status == nv;
          });
        }
      },
    },
  },
};
</script>
<style lang="scss" scoped>
.phone-home {
  .orderitem {
    width: 80%;
    margin: 5px auto;
    padding: 0 10px;
    height: 100px;
    background-color: #f8f8f8;
    border-radius: 10px;
    h4 {
      color: black;
    }
    .imgbox {
      // padding: 0 10px;
      width: 100%;
      height: 60px;
      img {
        border-radius: 6px;
        height: 60px;
        margin-right: 5px;
      }
    }
    p {
      display: flex;
      justify-content: space-between;
      color: black;
      .price {
        color: orangered;
        font-weight: bold;
      }
    }
  }
}
</style>
